# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=jj_tui
pkgver=0.8.9
pkgrel=0
pkgdesc="TUI for the Jujutsu version control system"
url="https://github.com/faldor20/jj_tui"
arch="all !armhf !armv7 !x86 !s390x" # jujutsu
license="MIT"
depends="jujutsu"
makedepends="
	bash
	dune
	linux-headers
	ocaml5-compiler-libs
	opam-monorepo
	"
subpackages="$pkgname-doc"
source="https://github.com/faldor20/jj_tui/archive/refs/tags/v$pkgver/jj_tui-$pkgver.tar.gz
	jj_tui.opam.locked

	missing-libs.patch
	opam-file.patch
	update-picos.patch
	"

export OPAMROOT="${OPAMROOT:-"$srcdir/opam-root"}"
export OPAMLOGS="${OPAMLOGS:-"$srcdir/opam-logs"}"

_opam_opts="-v -y --cli=2.2 --no-self-upgrade"
_lockfile_name="$pkgname.opam.locked"
_monorepo_opts="-v --lockfile=$_lockfile_name"

_minimal_update=true
recreate() {
	_minimal_update=false
}

opam_init() {
	opam init $_opam_opts \
		--compiler=ocaml-system \
		--disable-sandboxing \
		--no-setup
}

opam_lock() {
	[ -f "$OPAMROOT"/config ] || {
		opam_init

		opam repository $_opam_opts \
			add dune-universe \
			git+https://github.com/dune-universe/opam-overlays.git
	}

	cd "$startdir"
	[ -d "$builddir" ] || abuild unpack
	msg "Freezing $pkgname dependencies"

	# Resolve deps and update $pkgname.opam.locked, unless
	# recreate() is called, in which case create it afresh.
	(
		cd "$builddir" || {
			error 'Is $builddir set correctly?'
			return 1
		}
		opam update $_opam_opts

		if ! want_check; then
			_monorepo_opts="$_monorepo_opts --build-only"
		fi
		if $_minimal_update; then
			cp -v "$srcdir"/$_lockfile_name .

			_monorepo_opts="$_monorepo_opts --minimal-update"
		fi
		opam-monorepo lock $_monorepo_opts

		mv -v $_lockfile_name "$startdir"/
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	[ -f "$OPAMROOT"/config ] || opam_init
	ln -svf "$srcdir"/$_lockfile_name "$builddir"/
	opam-monorepo pull $_monorepo_opts --yes
}

build() {
	dune build --release --verbose
}

check() {
	dune runtest --build-dir=.testenv
}

package() {
	dune install --destdir="$pkgdir" --sections=bin,doc

	rm -v "$pkgdir"/usr/bin/widget_test
}

sha512sums="
bc18aaa716f19983511a122324ad2e0ccef0f687162d6bc0cc76017082367e4fb009ae84772052d62e7be8e135e8b459009d7089d7bd96fbd32e6dc0c3b596d5  jj_tui-0.8.9.tar.gz
d2fafc729f0e2dcc56778566af0f9152c286cbddd592a30cd9fd8ee3c7f761cdf9af4648a79b129f86013e3c6c4c14e45e270d914c267fff9cd4bfbf955808a8  jj_tui.opam.locked
44d8fffaa933574dc7bf840c21d1a63d46ce48a406d7fe95647e034b091f1d12dc0a93bc082018a9b41f16635ab104b9b6b53169b0067b63f4bc90edb5da9f44  missing-libs.patch
5bb6c1195afe12696e562732c481c8034e50183b2ebe6636182319fec2d09d8047212d8b9aea5b6a2f48cb6a2a1adb25dbdae80a577c5617e9ae2fa7059e34d3  opam-file.patch
f848b3d5d240b130ce44183e4345efcf01919fc22ff2b220e4cee4278519c39e8ec93280475906b54b3768633d74feb583b96ea79dc2ad5de5920b6f167711eb  update-picos.patch
"
